<?php
// +---------------------------------------------------------------------+
// | OneBase    | [ WE CAN DO IT JUST THINK ]                            |
// +---------------------------------------------------------------------+
// | Licensed   | http://www.apache.org/licenses/LICENSE-2.0 )           |
// +---------------------------------------------------------------------+
// | Author     | Bigotry <3162875@qq.com>                               |
// +---------------------------------------------------------------------+
// | Repository | https://gitee.com/Bigotry/OneBase                      |
// +---------------------------------------------------------------------+

namespace app\common\logic;

/**
 * 客户逻辑
 */
class Customer extends LogicBase
{
    
    /**
     * 获取客户列表
     */
    public function getCustomerList($where = [], $field = '*', $order = '')
    {
        
        $where[ DATA_STATUS_NAME] = ['neq', DATA_DELETE];

        return $this->modelCustomer->getList($where, $field, $order, 18);
    }
    
    /**
     * 获取客户列表搜索条件
     */
    public function getWhere($data = [])
    {
        
        $where = [];
        
        !empty($data['search_data']) && $where['a.campany_name|a.describe'] = ['like', '%'.$data['search_data'].'%'];
        
        return $where;
    }
    
    /**
     * 客户信息编辑
     */
    public function customerEdit($data = [])
    {
         
        $validate_result = $this->validateCustomer->scene('edit')->check($data);
        
        if (!$validate_result) {
            
            return [RESULT_ERROR, $this->validateCustomer->getError()];
        }

        $jump_url = url("CustomerList");

        $result = $this->modelCustomer->setInfo($data);
        
        $handle_text = empty($data['id']) ? '新增' : '编辑';
        
        $result && action_log($handle_text, '客户' . $handle_text . '，name：' . $data['campany_name']);
        
        return $result ? [RESULT_SUCCESS, '客户操作成功', $jump_url] : [RESULT_ERROR, $this->modelCustomer->getError()];
    }

    /**
     * 获取客户信息
     */
    public function getCustomerInfo($where = [], $field = 'a.*')
    {

        $this->modelCustomer->alias('a');
        
        $where['a.' . DATA_STATUS_NAME] = ['neq', DATA_DELETE];

        return $this->modelCustomer->getInfo($where, $field);
    }
    
   
    /**
     * 客户删除
     */
    public function customerDel($where = [])
    {
        
        $result = $this->modelCustomer->deleteInfo($where);
        
        $result && action_log('删除', '客户删除，where：' . http_build_query($where));
        
        return $result ? [RESULT_SUCCESS, '客户删除成功'] : [RESULT_ERROR, $this->modelCustomer->getError()];
    }
}
